package controller

import (
	"apifast/middleware"
	"apifast/pkg/ctx"

	"github.com/gin-gonic/gin"
)

type loginParam struct {
	UserName string `json:"userName" binding:"required,max=30"`
	Password string `json:"password" binding:"required,max=30"`
}

func LoginHandler(c *gin.Context) {
	var p loginParam
	if err := c.ShouldBindJSON(&p); err != nil {
		ctx.JSONWriteError(err, c)
		return
	}
	token, err := middleware.GenerateToken(1, 1, p.UserName)
	if err != nil {
		ctx.JSONWriteError(err, c)
		return
	}
	ctx.JSONWrite(gin.H{"token": token}, c)
}
